package com.imoblife.brainwavestus.pay;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender.SendIntentException;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.interfaces.IPayType;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.obpurchaseSystem.OBpurchaseSystem;
import com.imoblife.brainwavestus.obpurchaseSystem.OBpurchaseSystem.OBPurchaseNotifier;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.DebugUtil;
import com.imoblife.brainwavestus.util.DownloadUtil;
import com.imoblife.brainwavestus.util.MakePurchaseStateChenge;
import com.imoblife.brainwavestus.util.ProductUtil;

public class GoldPay implements IPayType {
	
	public static final String TAG = "GoldPay";
	
	private Activity mActivity;
	private Store mStore;
	private User mUser;
	
	public GoldPay(Activity activity) {
		
		mActivity = activity;
	}
	
	@Override
	public boolean pay(Product product) {		
		
		 showGetFreeDialog(product);
		 
		 return true;
	}
	
	public void setUser(User user) {
		
		mUser = user;
	}
	
	public void setStore(Store store) {
		
		mStore = store;
	}
	
	public void showGetFreeDialog(Product product) {
		
		ProductInfo info = product.getProductInfo();
		
		//name属性用于标记一个产品的唯一id,不随语言变化，title是跟随语言
		//变化的产品名称
		String productName = info.getTitle();
		final String productId = info.getId();
		String priceOfDollar = info.getPriceOfDollar();
		String priceOfGold = info.getPriceOfGold();
		
		
		DebugUtil.netDebug(TAG, 
				"productName:" + productName
				+ "priceOfDollar:" + priceOfDollar
				+ "priceOfGold:" + priceOfGold
				);

		String message = 
				mActivity.getString(R.string.get_free_massge)
				+ " " + productName + " (" 
				+ priceOfGold
				+ " "					
				+ mActivity.getString(R.string.buy_tus_credits) 
				+ ")?";

		new AlertDialog.Builder(mActivity)
				.setMessage(message)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								OBpurchaseSystem.getOBpurchaseSystem(
										mActivity).purchaseProduct(
										productId, 
										new OBPurchaseNotifier() {
											public void noticeSuccess(
													String tapjoyId) {	
												
												DebugUtil.debug(TAG, "pay success,begin update ui");
												
												Intent updateUi = new Intent(Constant.UPDATE_PRODUCT_BUY_UI_ACTION);												
												mActivity.sendBroadcast(updateUi);
												
												DownloadUtil.beginDownloadProduct(mActivity,
														mStore, 
														productId
														);											
																																	
												OBpurchaseSystem
														.getOBpurchaseSystem(
																mActivity)
														.updateTapPoints();
												DebugUtil.debug(TAG, "productId="
														+ productId + "buy success");
											}

											public void noticeFailed(
													String errorInfo) {
												DebugUtil.debug(TAG, errorInfo
														+ "buy fail");
											}
										}, mActivity);
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {															
							}
						}).show();
	}

}
